<?php 
/**
 * 
 * controlador de seguimiento generado
 * @author Natanael 
 * 
 */
class SeguimientoSend extends Controller{

      public function __construct()
      {
          parent::Controller();
          $this->load->library(array('chgraficos','chcalendario','chcompany'));
          
          $this->load->model(array('mseguimientosend', 'mdetallesend', 'usuarios','metapas','mservicios'));         
         
      }
      
      public function index()
      {   	   	
           $this->load->view('main', array('content_page' => 'headerss','menutop'=>'5'));
      }
      
      public function dataheader($id = false)
      {
           $page = $this->input->post('page') ? $this->input->post('page') : 1; // Pagina Actual o 1 por default
		   $sortname = $this->input->post('sidx') ? $this->input->post('sidx') : 'Fecha'; // Columna para el order by
		   $sortorder = $this->input->post('sord') ? $this->input->post('sord') : 'desc'; // Orden (asc|desc)
		   $limit = $this->input->post('rows') ? $this->input->post('rows') : 30;
		   $start = $limit * $page - $limit;
		   if($start < 0) $start = 0;
  
		    header("Expires: Mon, 26 Jul 1997 05:00:00 GMT" );
		    header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" );
		    header("Cache-Control: no-cache, must-revalidate" );
		    header("Pragma: no-cache" );

	      $records = $this->mseguimientosend->loadRegistro($start, $limit, false, false, $id);
	      
		  $totalRecords = count($records);
		

		  $totalPages = ceil($totalRecords/$limit);
		     if ($page > $totalPages) $page = $totalPages;

		   $json_output = array(
		       'page'		=> $page,
		       'total'		=> $totalPages,
		       'records'	=> $totalRecords,
		       'rows'		=> array()
		  );

		  if($records != false) {
		  	  foreach($records as $k) {
		  	  	$data = explode(" ",$k->getFecha());
		  	  	$data = explode("-",$data[0]);
		  	  	
		  	  	$data = $data[1].'/'.$data[2].'/'.$data[0];

		  	  	$usuario = $this->usuarios->LoadUsuarioById($k->getIdUsuario());
		  	  	
				$arr = $json_output['rows'][] = array(
				               'Id'          => $k->getIdSeguimiento(),
				               'Fecha'       => $data,
				               'Type'        => $k->getType(),
				               'usuario'     => $usuario->getNombreUsuario(),
				               'cant'        => $k->getCantidadSegimiento());
				
			}
		}

		$a=json_encode($json_output);
		$a=str_replace('\\','',$a);
		echo $a;
      }
      
      public function datadetalles()
      {
      	  $id = $this->input->post('iddetalle');
      	  $idType = $this->input->post('idtype');
      	  
          $page = $this->input->post('page') ? $this->input->post('page') : 1; // Pagina Actual o 1 por default
		  $sortname = $this->input->post('sidx') ? $this->input->post('sidx') : ''; // Columna para el order by
		  $sortorder = $this->input->post('sord') ? $this->input->post('sord') : 'desc'; // Orden (asc|desc)
		  $limit = $this->input->post('rows') ? $this->input->post('rows') : 10;
		  $start = $limit * $page - $limit;
		  if($start < 0) $start = 0;
  
		    header("Expires: Mon, 26 Jul 1997 05:00:00 GMT" );
		    header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" );
		    header("Cache-Control: no-cache, must-revalidate" );
		    header("Pragma: no-cache" );
         		    
	      $records = $this->mdetallesend->loadRegistro($id);
		
		  $totalRecords = count($records);
		
		  $totalPages = ceil($totalRecords/$limit);
		     if ($page > $totalPages) $page = $totalPages;

		   $json_output = array(
		       'page'		=> $page,
		       'total'		=> $totalPages,
		       'records'	=> $totalRecords,
		       'rows'		=> array()
		  );
		    
		  
		  if($records != false) {
		  	  foreach($records as $k) {
		  	  	
		  	  	if ($idType == 2) {
		  	  		$seguimiento = $this->mservicios->LoadServicioById($k->getIdEtapa());
		  	  		$etapa = $seguimiento->getNombreServicio();
		  	  	}else{
		  	  	   $etapa = $this->metapas->LoadEtapaById($k->getIdEtapa());
		  	  	   $etapa = $etapa->GetNombre();
		  	  	}
		  	  		  	
				$arr = $json_output['rows'][] = array(
				               'Referencia'        => $k->getReferencia(),
				               'Telefono'          => $k->getTelefono(),
				               'Contacto'          => $k->getContacto(),
				               'Etapa'             => $etapa
				        			               
				 );
				
			}
		  }

		    $a=json_encode($json_output);
		    $a=str_replace('\\','',$a);
		    echo $a;
      
      }
}